//
//  scanCreateController.m
//  operativeSide-2.0
//
//  Created by 王春程 on 2016/12/16.
//  Copyright © 2016年 王春程. All rights reserved.
//

#import "scanCreateController.h"

typedef NS_ENUM(NSInteger, INPUTTYPE) {
   INPUTTYPENAME = 0,
    INPUTTYPEPHONENUMBER,
    INPUTTYPECASH
};

@interface scanCreateController ()
@property (assign, nonatomic) INPUTTYPE inputType;
@property (weak, nonatomic) IBOutlet UITextField *nameTextField;
@property (weak, nonatomic) IBOutlet UITextField *connectField;
@property (weak, nonatomic) IBOutlet UIButton *choosePer;
@property (weak, nonatomic) IBOutlet UIButton *chooseCity;
@property (weak, nonatomic) IBOutlet UIButton *chooseStreet;
@property (weak, nonatomic) IBOutlet UIButton *payBtn;
@property (weak, nonatomic) IBOutlet UITextField *payTextField;
@property (weak, nonatomic) IBOutlet UIButton *backBtn;
@property (weak, nonatomic) IBOutlet UIButton *confirmBtn;
@property (assign, nonatomic) BOOL isPayBack;

@end

@implementation scanCreateController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self registerForKeyboardNotifications];
    [self initSubViews];
    
}

- (void)initSubViews {
    WS(weakSelf);
    [[self.backBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        [weakSelf.navigationController popViewControllerAnimated:YES];
    }];
    
    [[self.choosePer rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        
    }];
    
    [[self.chooseCity rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        
    }];
    
    [[self.chooseStreet rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        
    }];
    
    
    [[self.payBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        static BOOL isPayBack;
        isPayBack = !isPayBack;
        weakSelf.payBtn.selected = isPayBack;
        weakSelf.isPayBack = isPayBack;
    }];
    
    
    [[self.confirmBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        if([weakSelf isConform]) {
            NSLog(@"点击");
        } else {
            [weakSelf.view makeToast:@"请完善您的订单信息" duration:1.5 position:@"center"];
        }
    }];
}

- (BOOL)isConform {
    NSString *nameString = self.nameTextField.text;
    NSString *phoneString = self.connectField.text;
    NSString *payString = self.payTextField.text;
    NSString *perTitleString = self.choosePer.titleLabel.text;
    NSString *cityString = self.chooseCity.titleLabel.text;
    NSString *streetString = self.chooseStreet.titleLabel.text;
    
    return ((![nameString isEmptyString]) && (![phoneString isEmptyString]) && (![payString isEmptyString]) && (![perTitleString isEqualToString:@"选择省市"]) && (![cityString isEqualToString:@"选择城市"]) && (![streetString isEqualToString:@"选择街道"]));
}


- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    
    [self.nameTextField resignFirstResponder];
    [self.connectField resignFirstResponder];
    [self.payTextField resignFirstResponder];
}


- (void)registerForKeyboardNotifications

{
    
    [[NSNotificationCenter defaultCenter] addObserver:self
     
                                             selector:@selector(keyboardWasShown:)
     
                                                 name:UIKeyboardDidShowNotification object:nil];
    
    [[NSNotificationCenter defaultCenter] addObserver:self
     
                                             selector:@selector(keyboardWillBeHidden:)
     
                                                 name:UIKeyboardWillHideNotification object:nil];
    
}

//实现当键盘出现的时候计算键盘的高度大小。用于输入框显示位置
- (void)keyboardWasShown:(NSNotification*)aNotification
{
    WS(weakSelf);
    CGFloat duration = [aNotification.userInfo[@"UIKeyboardAnimationDurationUserInfoKey"] floatValue];
    CGRect frame = [aNotification.userInfo[@"UIKeyboardFrameEndUserInfoKey"] CGRectValue];
    
    CGFloat margin ;
    
    switch (self.inputType) {
        case INPUTTYPENAME:
        {
            margin = 113.0;
        }
            break;
        case INPUTTYPEPHONENUMBER:
        {
            margin = 156.0;
            
        }
            break;
        case INPUTTYPECASH:
        {
            margin = 339.0;
            
        }
            break;
        default:
            break;
    }
    
    NSLog(@"%f",margin);
    CGRect rect = self.view.bounds;
    rect.origin.y = margin - frame.origin.y;
    if (rect.origin.y > 0) {
        [UIView animateWithDuration:duration animations:^{
            NSLog(@"%f",rect.origin.y);
            weakSelf.view.bounds = rect;
        }];
    }
}

//当键盘隐藏的时候
- (void)keyboardWillBeHidden:(NSNotification*)aNotification
{
    WS(weakSelf);
    [UIView animateWithDuration:0.3 animations:^{
        CGRect rect = weakSelf.view.bounds;
        rect.origin.y = 0;
        weakSelf.view.bounds = rect;
    }];
}



- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self
                                                    name:UIKeyboardDidShowNotification
                                                  object:nil];
    
    
    [[NSNotificationCenter defaultCenter] removeObserver:self
                                                    name:UIKeyboardWillHideNotification
                                                  object:nil];
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

@end
